package com.scaffold.authority.action;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.authority.entity.User;
import com.scaffold.authority.service.UserService;

@Controller
public class UserJsonAction extends BasicBizAction{
	@Autowired
	private UserService userService;

	@RequestMapping("/user/list.json")
	public String list(String username, String password, String keyword){
		Page page = this.getPage(20);
		List<User> list = userService.pageQuery(page, username, password, keyword);
		this.filter(list, "password");
		return this.getPageResult(page, list);
	}
	
	@RequestMapping("/user/save.json")
	public String save(Long id){
		if(id==null){
			User user = new User();
			this.bindParams(user);
			this.setOwner(user);
			this.userService.save(user);
		}else{
			User user = this.userService.getUser(id);
			this.checkOwner(user);
			Map<String, String> map = this.getParameterMap();
			String password = map.get("password");
			if(password==null || password.isEmpty()){
				this.bindParams(user, "password");
			}else{
				this.bindParams(user);
			}
			this.userService.update(user);
		}
		return this.getSuccessResult();
	}

	@RequestMapping("/user/get.json")
	public String get(Long id){
		User user = userService.getUser(id);
		this.checkOwner(user);
		this.filter(user, "password");
		return this.getSuccessResult(user);
	}

	@RequestMapping("/user/delete.json")
	public String delete(Long id){
		User user = userService.getUser(id);
		this.checkOwner(user);
		this.userService.delete(user);
		return this.getSuccessResult();
	}

	@RequestMapping("/user/batchDelete.json")
	public String batchDelete(String idseq){
		this.userService.batchDelete(idseq);
		return this.getSuccessResult();
	}

	@RequestMapping("/user/select.json")
	public String select(String username, String password, String keyword){
		List<User> list = userService.pageQuery(null, username, password, keyword);
		return this.getSuccessResult(list);
	}

	/**
	 * 保存用户角色
	 * @param userId 用户id
	 * @param roleId 角色id序列
	 * @return
	 */
	@RequestMapping("/user/setting/saveRoles.json")
	public String saveRoles(Long userId, String roleId) {
		User user = this.userService.getUser(userId);
		this.checkOwner(user);
		userService.saveRoles(userId, roleId);
		return this.getSuccessResult();
	}

	/**
	 * 获取角色id列表
	 * @param userId 用户id
	 * @return
	 */
	@RequestMapping("/user/setting/getRoleIdList.json")
	public String getRoleIdList(Long userId) {
		User user = this.userService.getUser(userId);
		this.checkOwner(user);
		List<Long> list = userService.getRoleIdList(userId);
		return this.getSuccessResult(list);
	}

	/**
	 * 保存关系
	 * @param userId
	 * @param authorityId 
	 * @return
	 */
	@RequestMapping("/user/setting/saveAuthoritys.json")
	public String saveAuthoritys(Long userId, String authorityId) {
		User user = this.userService.getUser(userId);
		this.checkOwner(user);
		userService.saveAuthoritys(userId, authorityId);
		return this.getSuccessResult();
	}

	/**
	 * 获取用户关联的 id列表
	 * @param  userId
	 * @return
	 */
	@RequestMapping("/user/setting/getAuthorityIdList.json")
	public String getAuthorityIdList(Long userId) {
		User user = this.userService.getUser(userId);
		this.checkOwner(user);
		List<Long> list = userService.getAuthorityIdList(userId);
		return this.getSuccessResult(list);
	}
	
}

